/*************************************************************************
 *  Copyright © 2023-2030 FXB CO.,LTD. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：UPMProject
 *  文件：SvnPackageManagerSetting.cs
 *  作者：Administrator
 *  日期：2024/12/27 16:37:36
 *  功能：Nothing
*************************************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using SVNPackageManager.Runtime;

namespace SVNPackageManager.Editor
{
    public class SvnPackageManagerSetting : SettingsProvider
    {
        public static ScopedRegistryConfig scopedRegistryConfig;
        protected static SerializedObject scopedRegistrySerializedObject;
        protected static SerializedProperty usrNameProperty;
        protected static SerializedProperty passwordProperty;
        protected static SerializedProperty registrieAddressProperty;
        protected static SerializedProperty checkOutPathProperty;


        private static readonly GUIContent k_usrNameLabel_Text = new GUIContent("UserName");
        private static readonly GUIContent k_passwordLabel_Text = new GUIContent("Password");
        private static readonly GUIContent k_registrieLabel_Text = new GUIContent("Scoped Registries");
        private static readonly GUIContent k_checkOutLabel_Text = new GUIContent("Check Out Path");


        private GUIStyle authorizationStyle;
        private GUIStyle registrieStyle;


        private bool foldoutAuthorization = true;

        public SvnPackageManagerSetting(string path, SettingsScope scope = SettingsScope.Project)
            : base(path, scope) { }

        public override void OnActivate(string searchContext, VisualElement rootElement)
        {
            base.OnActivate(searchContext, rootElement);

            authorizationStyle = new GUIStyle("box");

            scopedRegistryConfig = Resources.Load<ScopedRegistryConfig>("ScopedRegistryConfig");
            scopedRegistrySerializedObject = new SerializedObject(scopedRegistryConfig);
            usrNameProperty = scopedRegistrySerializedObject.FindProperty("username");
            passwordProperty = scopedRegistrySerializedObject.FindProperty("password");
            registrieAddressProperty = scopedRegistrySerializedObject.FindProperty("registrieAddress");
            checkOutPathProperty = scopedRegistrySerializedObject.FindProperty("checkOutPath");
        }

        public override void OnGUI(string searchContext)
        {
            scopedRegistrySerializedObject.Update();

            EditorGUILayout.LabelField("Svn Package Manager", EditorStyles.boldLabel);

            EditorGUILayout.Space(10);

            EditorGUILayout.BeginVertical(new GUIStyle
            {
                normal = new GUIStyleState
                {
                    background = CreateTexture(1, 1, new Color(0.278f, 0.278f, 0.278f))

                },
                margin = new RectOffset(10, 10, 0, 0)
            });

            if (foldoutAuthorization = EditorGUILayout.BeginFoldoutHeaderGroup(foldoutAuthorization, "Authorization"))
            {
                //缩进
                EditorGUI.indentLevel++;

                EditorGUILayout.PropertyField(usrNameProperty, k_usrNameLabel_Text);
                EditorGUILayout.PropertyField(passwordProperty, k_passwordLabel_Text);

                EditorGUI.indentLevel--;
            }

            EditorGUILayout.EndFoldoutHeaderGroup();

            EditorGUILayout.EndVertical();

            Handles.color = Color.black;

            Rect rect1 = GUILayoutUtility.GetLastRect();

            DrawWireFrame(rect1);

            EditorGUILayout.Space(10);

            EditorGUILayout.BeginVertical(new GUIStyle
            {
                normal = new GUIStyleState
                {
                    background = CreateTexture(1, 1, new Color(0.278f, 0.278f, 0.278f))
                },
                margin = new RectOffset(10, 10, 0, 0)
            });

            EditorGUILayout.PropertyField(registrieAddressProperty, k_registrieLabel_Text);

            EditorGUILayout.EndVertical();

            Rect rect2 = GUILayoutUtility.GetLastRect();

            DrawWireFrame(rect2);

            EditorGUILayout.Space(10);

            EditorGUILayout.BeginVertical(new GUIStyle
            {
                normal = new GUIStyleState
                {
                    background = CreateTexture(1, 1, new Color(0.278f, 0.278f, 0.278f))

                },
                margin = new RectOffset(10, 10, 0, 0)
            });
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.PropertyField(checkOutPathProperty, k_checkOutLabel_Text);

            if (GUILayout.Button("选择",GUILayout.Width(80)))
            {
                checkOutPathProperty.stringValue = EditorUtility.OpenFolderPanel("选择文件夹", Application.dataPath, "");
            }

            EditorGUILayout.EndVertical();

            EditorGUILayout.EndVertical();

            Rect rect3 = GUILayoutUtility.GetLastRect();

            DrawWireFrame(rect3);

            scopedRegistrySerializedObject.ApplyModifiedProperties();

            EditorGUILayout.Space(10);
        }

        [SettingsProvider]
        public static SettingsProvider CreateUnityDefaultSettingProvider()
        {
            return new SvnPackageManagerSetting("Project/Svn Package Manager", SettingsScope.Project);
        }

        /// <summary>
        /// 创建一张纯色贴图
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        private Texture2D CreateTexture(int width, int height, Color color)
        {
            Texture2D texture = new Texture2D(width, height);
            for (int w = 0; w < width; w++)
            {
                for (int h = 0; h < height; h++)
                {
                    texture.SetPixel(w, h, color);
                }
            }
            texture.Apply();

            return texture;
        }

        private void DrawWireFrame(Rect rect)
        {
            Vector2 v1 = new Vector2(rect.x, rect.y);
            Vector2 v2 = new Vector2(rect.x + rect.width, rect.y);
            Vector2 v3 = new Vector2(rect.x, rect.y + rect.height);
            Vector2 v4 = new Vector2(rect.x + rect.width, rect.y + rect.height);

            Handles.DrawAAPolyLine(1.5f, v1, v2);
            Handles.DrawAAPolyLine(1.5f, v3, v4);
            Handles.DrawAAPolyLine(1.5f, v1, v3);
            Handles.DrawAAPolyLine(1.5f, v2, v4);

        }
    }
}


